Ticker

6/recent/ticker-posts

Python subjective question for competitive exam (5)

Q1.How will you concatenate multiple strings together in Python?

We can use following ways to concatenate multiple string together in Python: I. use + operator: E.g. >>> fname="John" >>> lname="Ray" >>> print fname+lname JohnRay II. use join function: E.g. >>> ''.join(['John','Ray']) 'JohnRay'

Q2. What is the use of Slicing in Python?

We can use Slicing in Python to get a substring from a String. The syntax of Slicing is very convenient to use. E.g. In following example we are getting a substring out of the name John. >>> name="John" >>> name[1:3] 'oh' In Slicing we can give two indices in the String to create a Substring. If we do not give first index, then it defaults to 0. E.g. >>> name="John" >>> name[:2] 'Jo' If we do not give second index, then it defaults to the size of the String. >>> name="John" >>> name[3:] 'n'



Q3. What is lambda expression in Python?

A lambda expression in Python is used for creating an anonymous function. Wherever we need a function, we can also use a lambda expression. We have to use lambda keyword for creating a lambda expression. Syntax of lambda function is as follows: lambda argumentList: expression E.g. lambda a,b: a+b The above mentioned lambda expression takes two arguments and returns their sum. We can use lambda expression to return a function. A lambda expression can be used to pass a function as an argument in another function.

Q4.What are the main benefits of using Python?

Some of the main benefits of using Python are as follows: I. Easy to learn: Python is simple language. It is easy to learn for a new programmer. II. Large library: There is a large library for utilities in Python that can be used for different kinds of applications. III. Readability: Python has a variety of statements and expressions that are quite readable and very explicit in their use. It increases the readability of overall code. IV. Memory management: In Python, memory management is built into the Interpreter. So a developer does not have to spend effort on managing memory among objects. V. Complex built-in Data types: Python has built-in Complex data types like list, set, dict etc. These data types give very good performance as well as save time in coding new features

Q5What is the use of // operator in Python?

Python provides // operator to perform floor division of a number by another. The result of // operator is a whole number (without decimal part) quotient that we get by dividing left number with right number. It can also be used floordiv(a,b). E.g. 10// 4 = 2 -10//4 = -3



Post a Comment

0 Comments